package com.zlt.appparking.service.impl;

import com.zlt.appparking.entity.DrivingLicense;
import com.zlt.appparking.entity.License;
import com.zlt.appparking.entity.LicenseExample;
import com.zlt.appparking.mapper.DrivingLicenseMapper;
import com.zlt.appparking.mapper.LicenseMapper;
import com.zlt.appparking.service.LicenseService;
import com.zlt.appparking.vo.ActiveUser;
import com.zlt.appparking.vo.LicenseVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class LicenseServiceImpl implements LicenseService {

    @Resource
    private LicenseMapper licenseMapper;

    @Autowired
    private DrivingLicenseMapper drivingLicenseMapper;

    /**
     * 车牌号码的正则表达式
     */
    public static final String PLATE_NO_REGEX = "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]" +
            "{1}(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{5})|([A-HJ-Z]{1}(([DF]{1}[A-HJ-NP-Z0-9]{1}[0-9]{4})|([0-9]{5}[DF]" +
            "{1})))|([A-HJ-Z]{1}[A-D0-9]{1}[0-9]{3}警)))|([0-9]{6}使)|((([沪粤川云桂鄂陕蒙藏黑辽渝]{1}A)|鲁B|闽D|蒙E|蒙H)[0-9]{4}领)" +
            "|(WJ[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼·•]{1}[0-9]{4}[TDSHBXJ0-9]{1})" +
            "|([VKHBSLJNGCE]{1}[A-DJ-PR-TVY]{1}[0-9]{5})";

    @Override
    public boolean bindLicense(String path, String num) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser == null){
            throw new RuntimeException("请登录后绑定");
        }
        boolean flag = "num".matches(PLATE_NO_REGEX);
        if (!flag){
            throw new RuntimeException("车牌格式不正确");
        }
        License license = new License();
        license.setlPath(path);
        license.setlNum(num);
        return true;
    }

    @Override
    public boolean bindLicensePlate(String number) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser == null){
            throw new RuntimeException("请登录后绑定");
        }
        boolean flag = number.matches(PLATE_NO_REGEX);//车牌格式校验
        if (!flag){
            throw new RuntimeException("车牌格式不正确");
        }
        LicenseExample licenseExample = new LicenseExample();
        License license = new License();
        List<Byte> states = new ArrayList<>();
        states.add((byte) 0);//待审核
        states.add((byte) 3);//已审核
        licenseExample.createCriteria().andLNumEqualTo(number).andLStateIn(states);
        List<License> licenses = licenseMapper.selectByExample(licenseExample);
        //不为空，且用户不匹配
        if (!licenses.isEmpty() && !licenses.get(0).getAuId().equals(activeUser.getAuId())){
            license.setAuId(activeUser.getAuId());
            license.setlNum(number);
            license.setlState((byte) 4);//账号被占用
            licenseMapper.insertSelective(license);
            throw new RuntimeException("该车牌已被绑定，若确认该车牌是您的，请进行车牌绑定申诉");
        } else if (!licenses.isEmpty() && licenses.get(0).getAuId().equals(activeUser.getAuId())){
            throw new RuntimeException("您已绑定该车牌，无需重复绑定");
        }
        //若数据不存在
        license.setlNum(number);//设置车牌
        license.setAuId(activeUser.getAuId());//设置用户
        int i = licenseMapper.insertSelective(license);
        return i > 0;
    }

    @Override
    public List<LicenseVO> licenses() {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser == null){
            throw new RuntimeException("请登录后查看");
        }
        LicenseExample licenseExample = new LicenseExample();
        licenseExample.createCriteria().andAuIdEqualTo(activeUser.getAuId());
        List<License> licenses = licenseMapper.selectByExample(licenseExample);
        List<LicenseVO> voList = new ArrayList<>();
        licenses.forEach(license -> {
            LicenseVO vo = new LicenseVO();
            BeanUtils.copyProperties(license,vo);
            voList.add(vo);
        });
        return voList;
    }

    @Override
    public boolean appealLicence(String path, Long lid) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser == null){
            throw new RuntimeException("请登录后申诉");
        }
        DrivingLicense drivingLicense =new DrivingLicense();
        drivingLicense.setAuId(activeUser.getAuId());
        drivingLicense.setAuId("1");
        drivingLicense.setDlPhoto(path);
        drivingLicense.setlId(lid);
        return drivingLicenseMapper.insertSelective(drivingLicense) >0;
    }

    @Override
    public LicenseVO selectLicense(Long id) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser == null){
            throw new RuntimeException("请登录后申诉");
        }
        License license = licenseMapper.selectByPrimaryKey(id);
        //todo 查询车牌数据时，应当查出绑定人
        LicenseVO licenseVO = new LicenseVO();
        BeanUtils.copyProperties(license,licenseVO);
        return licenseVO;
    }


}
